﻿//TODO: Add: Test

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// Adding two different bitmaps
    /// </summary>
    public class Add : BaseImageFilter, ITwoSourceFilter
    {
        protected delegate Color ColorCalculationDelegate(Color color1, Color color2);

        /// <summary>
        /// Initializes a new instance of the <see cref="Add"/> class.
        /// </summary>
        public Add()
        {
            m_bClipColor = true;
            M_PROCESSING_MODE = PROCESSING_MODE.GrayValue;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Add"/> class.
        /// </summary>
        /// <param name="_add">The _add.</param>
        internal Add(Add _add)
        {
            m_bClipColor = _add.m_bClipColor;
            M_PROCESSING_MODE = _add.M_PROCESSING_MODE;
        }

        /// <summary>
        /// Execute this Filter
        /// A Debug Statement is written if a concrete Filter is called
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            Process((UnsafeBitmap)_bitmap.Clone(), _bitmap);
            return _bitmap;
        }

        /// <summary>
        /// Adding two bitmaps
        /// </summary>
        /// <param name="_bitmap1"></param>
        /// <param name="_bitmap2"></param>
        /// <returns></returns>
        public UnsafeBitmap Process(UnsafeBitmap _bitmap1, UnsafeBitmap _bitmap2)
        {
            int y = 0;
            int x = 0;
            int nWidth = _bitmap1.Width;
            int nHeight = _bitmap1.Height;

            UnsafeBitmap bitmap = new Bitmap(nWidth, nHeight);

            Color color1 = Color.Transparent;
            Color color2 = Color.Transparent;

            ColorCalculationDelegate colorCalculation = null;
            switch (M_PROCESSING_MODE)
            {
                case PROCESSING_MODE.GrayColor:
                case PROCESSING_MODE.GrayValue:
                    colorCalculation = new ColorCalculationDelegate(this.GrayAdd);
                    break;

                case PROCESSING_MODE.Color:
                    colorCalculation = new ColorCalculationDelegate(this.ColorAdd);
                    break;
            }

            Color color = Color.Transparent;

            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    color1 = _bitmap1.GetPixel(x, y);
                    color2 = _bitmap2.GetPixel(x, y);

                    color = colorCalculation(color1, color2);

                    bitmap.SetPixel(x, y, color);
                }
            }

            return bitmap;
        }

        /// <summary>
        /// Add Grays
        /// </summary>
        /// <param name="color1"></param>
        /// <param name="color2"></param>
        /// <returns></returns>
        protected Color GrayAdd(Color color1, Color color2)
        {
            int nGray = ColorFunctions.Gray(color1) + ColorFunctions.Gray(color2);
            if (m_bClipColor)
            {
                if (nGray > 255) nGray = 255;
            }
            return Color.FromArgb(nGray, nGray, nGray);
        }

        /// <summary>
        /// Add Colors
        /// </summary>
        /// <param name="color1"></param>
        /// <param name="color2"></param>
        /// <returns></returns>
        protected Color ColorAdd(Color color1, Color color2)
        {
            int nRGB = color1.ToArgb() + color2.ToArgb();
            if (nRGB > int.MaxValue) nRGB = int.MaxValue;
            return Color.FromArgb(nRGB);
        }

         /// <summary>
        /// Adding two bitmaps
        /// </summary>
        /// <param name="_bitmap1"></param>
        /// <param name="_bConstant"></param>
        /// <returns></returns>
        public UnsafeBitmap Process(UnsafeBitmap _bitmap1, byte _bConstant)
        {
            int y = 0;
            int x = 0;
            int nWidth = _bitmap1.Width;
            int nHeight = _bitmap1.Height;

            UnsafeBitmap bitmap = new Bitmap(nWidth, nHeight);

            Color color1 = Color.Transparent;

            m_ColorCalculation = Definitions.ColorCalculationDictionaryProcessingMode[M_PROCESSING_MODE];
            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    color1 = _bitmap1.GetPixel(x,y);
                    float fColor1 = m_ColorCalculation(color1);
                    if (m_bClipColor)
                    {
                    }
                    bitmap.SetPixel(x, y, Color.FromArgb((int)fColor1));
                }
            }
            return bitmap;
        }

        /// <summary>
        /// Adding two bitmaps
        /// </summary>
        /// <param name="_bitmap1"></param>
        /// <param name="_bitmap2"></param>
        /// <returns></returns>
        public UnsafeBitmap Process(UnsafeBitmap _bitmap1, int _nConstant)
        {
            int y = 0;
            int x = 0;
            int nWidth = _bitmap1.Width;
            int nHeight = _bitmap1.Height;

            UnsafeBitmap bitmap = new Bitmap(nWidth, nHeight);

            Color color1 = Color.Transparent;
            Color color2 = Color.Transparent;
            int nR = -1;
            int nG = -1;
            int nB = -1;

            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    bitmap.SetPixel(x, y, Color.FromArgb(bitmap.GetPixel(x,y).ToArgb() + _nConstant));
                }
            }
            return bitmap;
        }

        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new Add(this);
        }
    }
}
